package com.capinfo.accounting.web;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.NativeWebRequest;

import com.alibaba.fastjson.JSON;
import com.capinfo.accumulation.model.accounting.Fzhsxmjbsj;
import com.capinfo.accumulation.model.accounting.Fzlxsz;
import com.capinfo.accumulation.model.accounting.Kmszjbsj;
import com.capinfo.accumulation.parameter.accounting.FzhsxmjbsjParameter;
import com.capinfo.accumulation.service.accounting.FzhsxmjbsjService;
import com.capinfo.accumulation.service.accounting.FzlxszService;
import com.capinfo.framework.model.dictionary.Dictionary;
import com.capinfo.framework.model.dictionary.DictionarySort;
import com.capinfo.framework.service.CommonsDataOperationService;
import com.capinfo.framework.web.controller.CommonsDataManageController;
import com.capinfo.framework.web.util.ControllerUtils;

@Controller
@RequestMapping("/business/accounting/fuzhuhesuan")
public class FuZhuHeSuanController extends CommonsDataManageController<FzhsxmjbsjParameter,Fzhsxmjbsj>{
    @Autowired
	private FzhsxmjbsjService fzhsxmjbsjService;
    @Autowired
   	private FzlxszService fzlxszService;

	@Override
	public CommonsDataOperationService<Fzhsxmjbsj, FzhsxmjbsjParameter> getCommonsDataOperationService() {
		return fzhsxmjbsjService;
	}
	@RequestMapping({ "/search.shtml"})
	public Map<String, Object> search(FzhsxmjbsjParameter parameter) {
		Map<String, Object> viewModel = ControllerUtils.buildViewModel(parameter);
		List< Fzlxsz>  fzlxszList = fzlxszService.getFzlxsz();
		viewModel.put("fzlxszList", fzlxszList);
		return viewModel;
	}
	/**
	 * 会计年度下拉框
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping({ "/kjnd.shtml" })
	@ResponseBody
	public void kjnd(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		Set<Dictionary> list = fzhsxmjbsjService.getDictionariesBySortName("会计年度");
		List<Map<String, String>> li =new ArrayList<Map<String, String>>();
		
		Iterator<Dictionary> it = list.iterator();  
		while (it.hasNext()) {  
		  Dictionary dic = it.next();
		  Map<String, String> mp = new HashMap<String, String>();
		  mp.put("id", String.valueOf(dic.getId()));
		  mp.put("code", dic.getCode());
		  mp.put("name", dic.getName());
		  li.add(mp);
		}  
		PrintWriter pw;
		pw = response.getWriter();
		pw.write(JSON.toJSONString(li));
	}
	/**
	 * 核算类型下拉框
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping({ "/hslx.shtml" })
	@ResponseBody
	public void hslx(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		List< Fzlxsz>  fzlxszList = fzlxszService.getFzlxsz();	
		PrintWriter pw;
		pw = response.getWriter();
		pw.write(JSON.toJSONString(fzlxszList));
	}
	/**
	 *  财务科目下拉框
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	@RequestMapping({ "/cwkm.shtml" })
	@ResponseBody
	public void cwkm(HttpServletRequest request,HttpServletResponse response) throws IOException {
		response.setHeader("Content-type", "text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		List<Kmszjbsj>  kmszList =  fzhsxmjbsjService.findKmsz();
		PrintWriter pw;
		pw = response.getWriter();
		pw.write(JSON.toJSONString(kmszList));
	}
	@Override
	protected Map<String, Object> formAfterInvoke(NativeWebRequest request, FzhsxmjbsjParameter parameter,
	    Map<String, Object> viewModel) {
		Set<Dictionary> kjndList = fzhsxmjbsjService.getDictionariesBySortName("会计年度");
		viewModel.put("kjndList", kjndList);
		List< Fzlxsz>  fzlxszList = fzlxszService.getFzlxsz();
		viewModel.put("fzlxszList", fzlxszList);
		return super.formAfterInvoke(request, parameter, viewModel);
	}
	@RequestMapping("/deletexg.shtml")
	public final void deletexg(NativeWebRequest request, FzhsxmjbsjParameter parameter, Writer writer) {
		ControllerUtils.outputByWriter(request, writer, getCommonsDataOperationService().delete(parameter));
		
		
	}
}
